/** * 试卷列表 */

<template>
  <div class="courseBox">
    <div class="courseBoxTop">
      <!-- 搜索 -->
      <div class="courseBoxTopSearch">
        <div class="searchItem" id="courseName">
          <span>试卷ID：</span>
          <el-input v-model="studentName" />
        </div>
        <div class="searchItem" id="papers">
          <span>年级:</span>
          <el-select v-model="gradeId" placeholder="请选择年级">
            <el-option
              v-for="item in gradeList"
              :key="item.value"
              :label="item.typeName"
              :value="item.typeId"
            >
            </el-option>
          </el-select>
        </div>
        <div class="searchItem" id="profession">
          <span>学科：</span>
          <el-select v-model="subjectId" placeholder="请选择学科">
            <el-option
              :label="item.typeName"
              :value="item.typeId"
              v-for="(item, index) in subjects"
              :key="index"
            >
            </el-option>
          </el-select>
        </div>
        <div class="searchAndReset">
          <el-button class="searchBtn" @click="search">
            <i class="el-icon-search"></i>
            <span>查询</span>
          </el-button>
          <el-button class="searchBtn" @click="chongzhi">
            <i class="el-icon-search"></i>
            <span>重置</span>
          </el-button>
        </div>

        <div class="searchAndReset">
          <el-button class="searchBtn" @click="zidong">
            <!-- <i class="el-icon-search"></i> -->
            <span>自动分卷</span>
          </el-button>
          <el-button class="resetBtn" @click="toCreatePager">
            <!-- <i class="el-icon-refresh"></i> -->
            <span>创建试卷</span>
          </el-button>
        </div>
      </div>
    </div>

    <!-- 管理列表 -->
    <div class="courseBoxBottom">
      <!-- 表格 -->
      <div class="bottomMian">
        <el-table
          ref="multipleTable"
          :data="studentStatusList"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column prop="id" label="id"></el-table-column>
          <el-table-column prop="subjectId" label="学科"> </el-table-column>
          <el-table-column prop="paperName" label="名称"></el-table-column>
          <el-table-column prop="createTime" label="创建时间"></el-table-column>
          <el-table-column prop="action" label="操作">
            <div class="actionImg" slot-scope="scope">
              <el-button
                type="primary"
                size="small"
                @click="goCheckStudentStatusPage(scope.row, 'edit')"
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="small"
                @click="delStudentStatus(scope.row)"
                >删除</el-button
              >
            </div>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog title="自动分卷" :visible.sync="dialogTableVisible">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :header-cell-style="{ textAlign: 'center', background: '#f3f8fc' }"
        :cell-style="{ textAlign: 'center' }"
      >
        <el-table-column label="序号" type="index" width="50">
        </el-table-column>
        <el-table-column label="名称" prop="paperName"> </el-table-column>
        <el-table-column label="年级" prop="gradeId"> </el-table-column>
        <el-table-column prop="subjectId" label="学科"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column prop="difficultyName" label="试卷难度" width="100">
        </el-table-column>
        <el-table-column prop="address" label="操作" width="100">
          <template slot-scope="scope">
            <div class="haed">
              <el-button
                size="small"
                type="success"
                class="refreshBtn"
                @click="auto(scope.row)"
                >选择</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import "@/styles/picture.scss";
import { getCourseListApi } from "@/api/basics/course.js";
import { getGradeListApi } from "@/api/basics/teachingPlan.js";
import {
  getTextPagerListApi,
  delTextPagerApi,
  addAutoApi,
} from "../../../api/onlinehomework/textPager.js";
import { getSubsectionRulesDataApi } from "@/api/onlinehomework/subsectionRules.js";
export default {
  data() {
    return {
      studentStatusList: [],
      total: 0,
      gradeId: "",
      subjectId: "",
      studentName: "",
      currentPage: 1,
      pageSize: 10,
      pageNum: 1,
      form: {
        name: "",
        date: "",
        pixel: "",
        size: "",
      },
      gradeList: [],
      subjects: [],
      dialogTableVisible: false,
      tableData: [],
    };
  },
  mounted() {
    this.getPagerList();
    this.getKecheng();
    this.getSubsectionRulesDataList();
  },
  methods: {
    chongzhi() {
      this.gradeId = "";
      this.subjectId = "";
      this.studentName = "";
      this.getPagerList();
    },
    async auto(row) {
      const data = await addAutoApi({ id: row.id });
      console.log(data);
      if (data.code == 200) {
        this.$message({
          message: "创建成功",
          type: "success",
        });
        this.dialogTableVisible = false;
      }
    },
    handleCurrentChange(e) {
      this.pageNum = e;
      this.getPagerList();
    },
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPagerList();
    },
    getSubsectionRulesDataList() {
      getSubsectionRulesDataApi().then((res) => {
        console.log(res, "-----");
        this.tableData = res.rows;
        console.log(this.tableData);
      });
    },
    zidong() {
      this.dialogTableVisible = true;
    },
    async search() {
      let obj = {
        subjectId: this.subjectId,
        gradeId: this.gradeId,
        id: this.studentName,
      };
      let obj1 = {};
      for (let key in obj) {
        if (
          (obj[key] === 0 || obj[key]) &&
          obj[key].toString().replace(/(^\s*)|(\s*$)/g, "") !== ""
        ) {
          // 保存属性
          obj1[key] = obj[key];
        }
      }
      console.log(obj1, "obj1");

      const data = await getTextPagerListApi(obj1);
      const datas = await getCourseListApi();
      console.log(datas.rows);
      this.studentStatusList = data.rows;
      this.total = data.total;
      this.studentStatusList.forEach((item) => {
        datas.rows.forEach((ite) => {
          if (item.subjectId == ite.courseId) {
            item.subjectId = ite.courseName;
          }
        });
      });
      console.log(data);
    },
    async getKecheng() {
      const datas = await getGradeListApi({ parentId: 130 });
      const data1 = await getGradeListApi({ parentId: 55 });
      this.gradeList = data1.rows;
      this.subjects = datas.rows;
    },
    // 修改试卷
    goCheckStudentStatusPage(row) {
      this.$router.push(
        "/onlinehomework/testpaper/createtestpaper?id=" + row.id
      );
    },
    // 删除试卷
    delStudentStatus(row) {
      this.$confirm("此操作将永久删除该试卷, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await delTextPagerApi({ id: row.id });
          this.getPagerList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取试卷列表
    async getPagerList() {
      const data = await getTextPagerListApi({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
      });
      console.log(data.rows);
      this.total = data.total;
      const datas = await getCourseListApi();
      console.log(data.rows);
      data.rows.sort((a, b) => {
        return (
          Date.parse(new Date(b.createTime)) -
          Date.parse(new Date(a.createTime))
        );
      });
      this.studentStatusList = data.rows;
      this.studentStatusList.forEach((item) => {
        datas.rows.forEach((ite) => {
          if (item.subjectId == ite.courseId) {
            item.subjectId = ite.courseName;
          }
        });
      });
      console.log(this.studentStatusList);
    },
    //   跳转到创建试卷页面
    toCreatePager() {
      console.log(555);
      this.$router.push("/onlinehomework/testpaper/createtestpaper");
    },
  },
};
</script>

<style lang="scss" scoped>
.courseBoxTopSearch {
  ::v-deep .el-input__inner {
    height: 36px;
    line-height: 36px;
  }

  .searchItem {
    float: left;
    display: flex;
    align-items: center;
    margin-right: 50px;
    margin-bottom: 20px;

    span {
      width: 100px;
      font-size: 14px;
      text-align: right;
    }
  }

  #courseName {
    float: left;
    display: flex;
    align-items: center;
    margin-right: 20px;
    margin-bottom: 20px;
  }

  #papers {
    margin-right: 20px;
    span {
      width: 140px;
      font-size: 14px;
      text-align: right;
    }
  }

  #profession {
    float: left;
    display: flex;
    align-items: center;
    margin-right: 20px;
    margin-bottom: 20px;
  }

  #version {
    float: left;
    display: flex;
    align-items: center;
    margin-bottom: 20px;
  }
}

.actionImg {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;

  .editImg {
    margin-left: 20px;
  }

  .delectImg {
    margin: 0 10px;
  }

  span {
    font-size: 12px;
    color: #4b93d5;
  }
}

::v-deep .el-icon-date {
  line-height: 36px;
}
</style>
